/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Recordlevelelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Type;
import entityBeans.Ownerinstitution;
import entityBeans.Language;
import entityBeans.Institutioncode;
import entityBeans.Dataset;
import entityBeans.Collectioncode;
import entityBeans.Basisofrecord;
import entityBeans.Dynamicproperty;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Specimen;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class RecordlevelelementJpaController {

    public RecordlevelelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Recordlevelelement recordlevelelement) {
        if (recordlevelelement.getDynamicpropertyList() == null) {
            recordlevelelement.setDynamicpropertyList(new ArrayList<Dynamicproperty>());
        }
        if (recordlevelelement.getSpecimenList() == null) {
            recordlevelelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Type type = recordlevelelement.getType();
            if (type != null) {
                type = em.getReference(type.getClass(), type.getIdtype());
                recordlevelelement.setType(type);
            }
            Ownerinstitution ownerinstitution = recordlevelelement.getOwnerinstitution();
            if (ownerinstitution != null) {
                ownerinstitution = em.getReference(ownerinstitution.getClass(), ownerinstitution.getIdownerinstitution());
                recordlevelelement.setOwnerinstitution(ownerinstitution);
            }
            Language language = recordlevelelement.getLanguage();
            if (language != null) {
                language = em.getReference(language.getClass(), language.getIdlanguage());
                recordlevelelement.setLanguage(language);
            }
            Institutioncode institutioncode = recordlevelelement.getInstitutioncode();
            if (institutioncode != null) {
                institutioncode = em.getReference(institutioncode.getClass(), institutioncode.getIdinstitutioncode());
                recordlevelelement.setInstitutioncode(institutioncode);
            }
            Dataset dataset = recordlevelelement.getDataset();
            if (dataset != null) {
                dataset = em.getReference(dataset.getClass(), dataset.getIddataset());
                recordlevelelement.setDataset(dataset);
            }
            Collectioncode collectioncode = recordlevelelement.getCollectioncode();
            if (collectioncode != null) {
                collectioncode = em.getReference(collectioncode.getClass(), collectioncode.getIdcollectioncode());
                recordlevelelement.setCollectioncode(collectioncode);
            }
            Basisofrecord basisofrecord = recordlevelelement.getBasisofrecord();
            if (basisofrecord != null) {
                basisofrecord = em.getReference(basisofrecord.getClass(), basisofrecord.getIdbasisofrecord());
                recordlevelelement.setBasisofrecord(basisofrecord);
            }
            List<Dynamicproperty> attachedDynamicpropertyList = new ArrayList<Dynamicproperty>();
            for (Dynamicproperty dynamicpropertyListDynamicpropertyToAttach : recordlevelelement.getDynamicpropertyList()) {
                dynamicpropertyListDynamicpropertyToAttach = em.getReference(dynamicpropertyListDynamicpropertyToAttach.getClass(), dynamicpropertyListDynamicpropertyToAttach.getIddynamicproperty());
                attachedDynamicpropertyList.add(dynamicpropertyListDynamicpropertyToAttach);
            }
            recordlevelelement.setDynamicpropertyList(attachedDynamicpropertyList);
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : recordlevelelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            recordlevelelement.setSpecimenList(attachedSpecimenList);
            em.persist(recordlevelelement);
            if (type != null) {
                type.getRecordlevelelementList().add(recordlevelelement);
                type = em.merge(type);
            }
            if (ownerinstitution != null) {
                ownerinstitution.getRecordlevelelementList().add(recordlevelelement);
                ownerinstitution = em.merge(ownerinstitution);
            }
            if (language != null) {
                language.getRecordlevelelementList().add(recordlevelelement);
                language = em.merge(language);
            }
            if (institutioncode != null) {
                institutioncode.getRecordlevelelementList().add(recordlevelelement);
                institutioncode = em.merge(institutioncode);
            }
            if (dataset != null) {
                dataset.getRecordlevelelementList().add(recordlevelelement);
                dataset = em.merge(dataset);
            }
            if (collectioncode != null) {
                collectioncode.getRecordlevelelementList().add(recordlevelelement);
                collectioncode = em.merge(collectioncode);
            }
            if (basisofrecord != null) {
                basisofrecord.getRecordlevelelementList().add(recordlevelelement);
                basisofrecord = em.merge(basisofrecord);
            }
            for (Dynamicproperty dynamicpropertyListDynamicproperty : recordlevelelement.getDynamicpropertyList()) {
                dynamicpropertyListDynamicproperty.getRecordlevelelementList().add(recordlevelelement);
                dynamicpropertyListDynamicproperty = em.merge(dynamicpropertyListDynamicproperty);
            }
            for (Specimen specimenListSpecimen : recordlevelelement.getSpecimenList()) {
                Recordlevelelement oldRecordlevelelementOfSpecimenListSpecimen = specimenListSpecimen.getRecordlevelelement();
                specimenListSpecimen.setRecordlevelelement(recordlevelelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldRecordlevelelementOfSpecimenListSpecimen != null) {
                    oldRecordlevelelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldRecordlevelelementOfSpecimenListSpecimen = em.merge(oldRecordlevelelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Recordlevelelement recordlevelelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Recordlevelelement persistentRecordlevelelement = em.find(Recordlevelelement.class, recordlevelelement.getIdrecordlevelelement());
            Type typeOld = persistentRecordlevelelement.getType();
            Type typeNew = recordlevelelement.getType();
            Ownerinstitution ownerinstitutionOld = persistentRecordlevelelement.getOwnerinstitution();
            Ownerinstitution ownerinstitutionNew = recordlevelelement.getOwnerinstitution();
            Language languageOld = persistentRecordlevelelement.getLanguage();
            Language languageNew = recordlevelelement.getLanguage();
            Institutioncode institutioncodeOld = persistentRecordlevelelement.getInstitutioncode();
            Institutioncode institutioncodeNew = recordlevelelement.getInstitutioncode();
            Dataset datasetOld = persistentRecordlevelelement.getDataset();
            Dataset datasetNew = recordlevelelement.getDataset();
            Collectioncode collectioncodeOld = persistentRecordlevelelement.getCollectioncode();
            Collectioncode collectioncodeNew = recordlevelelement.getCollectioncode();
            Basisofrecord basisofrecordOld = persistentRecordlevelelement.getBasisofrecord();
            Basisofrecord basisofrecordNew = recordlevelelement.getBasisofrecord();
            List<Dynamicproperty> dynamicpropertyListOld = persistentRecordlevelelement.getDynamicpropertyList();
            List<Dynamicproperty> dynamicpropertyListNew = recordlevelelement.getDynamicpropertyList();
            List<Specimen> specimenListOld = new ArrayList<Specimen>();//persistentRecordlevelelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>();//recordlevelelement.getSpecimenList();
            if (typeNew != null) {
                typeNew = em.getReference(typeNew.getClass(), typeNew.getIdtype());
                recordlevelelement.setType(typeNew);
            }
            if (ownerinstitutionNew != null) {
                ownerinstitutionNew = em.getReference(ownerinstitutionNew.getClass(), ownerinstitutionNew.getIdownerinstitution());
                recordlevelelement.setOwnerinstitution(ownerinstitutionNew);
            }
            if (languageNew != null) {
                languageNew = em.getReference(languageNew.getClass(), languageNew.getIdlanguage());
                recordlevelelement.setLanguage(languageNew);
            }
            if (institutioncodeNew != null) {
                institutioncodeNew = em.getReference(institutioncodeNew.getClass(), institutioncodeNew.getIdinstitutioncode());
                recordlevelelement.setInstitutioncode(institutioncodeNew);
            }
            if (datasetNew != null) {
                datasetNew = em.getReference(datasetNew.getClass(), datasetNew.getIddataset());
                recordlevelelement.setDataset(datasetNew);
            }
            if (collectioncodeNew != null) {
                collectioncodeNew = em.getReference(collectioncodeNew.getClass(), collectioncodeNew.getIdcollectioncode());
                recordlevelelement.setCollectioncode(collectioncodeNew);
            }
            if (basisofrecordNew != null) {
                basisofrecordNew = em.getReference(basisofrecordNew.getClass(), basisofrecordNew.getIdbasisofrecord());
                recordlevelelement.setBasisofrecord(basisofrecordNew);
            }
            List<Dynamicproperty> attachedDynamicpropertyListNew = new ArrayList<Dynamicproperty>();
            for (Dynamicproperty dynamicpropertyListNewDynamicpropertyToAttach : dynamicpropertyListNew) {
                dynamicpropertyListNewDynamicpropertyToAttach = em.getReference(dynamicpropertyListNewDynamicpropertyToAttach.getClass(), dynamicpropertyListNewDynamicpropertyToAttach.getIddynamicproperty());
                attachedDynamicpropertyListNew.add(dynamicpropertyListNewDynamicpropertyToAttach);
            }
            dynamicpropertyListNew = attachedDynamicpropertyListNew;
            recordlevelelement.setDynamicpropertyList(dynamicpropertyListNew);
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            recordlevelelement.setSpecimenList(specimenListNew);
            recordlevelelement = em.merge(recordlevelelement);
            if (typeOld != null && !typeOld.equals(typeNew)) {
                typeOld.getRecordlevelelementList().remove(recordlevelelement);
                typeOld = em.merge(typeOld);
            }
            if (typeNew != null && !typeNew.equals(typeOld)) {
                typeNew.getRecordlevelelementList().add(recordlevelelement);
                typeNew = em.merge(typeNew);
            }
            if (ownerinstitutionOld != null && !ownerinstitutionOld.equals(ownerinstitutionNew)) {
                ownerinstitutionOld.getRecordlevelelementList().remove(recordlevelelement);
                ownerinstitutionOld = em.merge(ownerinstitutionOld);
            }
            if (ownerinstitutionNew != null && !ownerinstitutionNew.equals(ownerinstitutionOld)) {
                ownerinstitutionNew.getRecordlevelelementList().add(recordlevelelement);
                ownerinstitutionNew = em.merge(ownerinstitutionNew);
            }
            if (languageOld != null && !languageOld.equals(languageNew)) {
                languageOld.getRecordlevelelementList().remove(recordlevelelement);
                languageOld = em.merge(languageOld);
            }
            if (languageNew != null && !languageNew.equals(languageOld)) {
                languageNew.getRecordlevelelementList().add(recordlevelelement);
                languageNew = em.merge(languageNew);
            }
            if (institutioncodeOld != null && !institutioncodeOld.equals(institutioncodeNew)) {
                institutioncodeOld.getRecordlevelelementList().remove(recordlevelelement);
                institutioncodeOld = em.merge(institutioncodeOld);
            }
            if (institutioncodeNew != null && !institutioncodeNew.equals(institutioncodeOld)) {
                institutioncodeNew.getRecordlevelelementList().add(recordlevelelement);
                institutioncodeNew = em.merge(institutioncodeNew);
            }
            if (datasetOld != null && !datasetOld.equals(datasetNew)) {
                datasetOld.getRecordlevelelementList().remove(recordlevelelement);
                datasetOld = em.merge(datasetOld);
            }
            if (datasetNew != null && !datasetNew.equals(datasetOld)) {
                datasetNew.getRecordlevelelementList().add(recordlevelelement);
                datasetNew = em.merge(datasetNew);
            }
            if (collectioncodeOld != null && !collectioncodeOld.equals(collectioncodeNew)) {
                collectioncodeOld.getRecordlevelelementList().remove(recordlevelelement);
                collectioncodeOld = em.merge(collectioncodeOld);
            }
            if (collectioncodeNew != null && !collectioncodeNew.equals(collectioncodeOld)) {
                collectioncodeNew.getRecordlevelelementList().add(recordlevelelement);
                collectioncodeNew = em.merge(collectioncodeNew);
            }
            if (basisofrecordOld != null && !basisofrecordOld.equals(basisofrecordNew)) {
                basisofrecordOld.getRecordlevelelementList().remove(recordlevelelement);
                basisofrecordOld = em.merge(basisofrecordOld);
            }
            if (basisofrecordNew != null && !basisofrecordNew.equals(basisofrecordOld)) {
                basisofrecordNew.getRecordlevelelementList().add(recordlevelelement);
                basisofrecordNew = em.merge(basisofrecordNew);
            }
            for (Dynamicproperty dynamicpropertyListOldDynamicproperty : dynamicpropertyListOld) {
                if (!dynamicpropertyListNew.contains(dynamicpropertyListOldDynamicproperty)) {
                    dynamicpropertyListOldDynamicproperty.getRecordlevelelementList().remove(recordlevelelement);
                    dynamicpropertyListOldDynamicproperty = em.merge(dynamicpropertyListOldDynamicproperty);
                }
            }
            for (Dynamicproperty dynamicpropertyListNewDynamicproperty : dynamicpropertyListNew) {
                if (!dynamicpropertyListOld.contains(dynamicpropertyListNewDynamicproperty)) {
                    dynamicpropertyListNewDynamicproperty.getRecordlevelelementList().add(recordlevelelement);
                    dynamicpropertyListNewDynamicproperty = em.merge(dynamicpropertyListNewDynamicproperty);
                }
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setRecordlevelelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Recordlevelelement oldRecordlevelelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getRecordlevelelement();
                    specimenListNewSpecimen.setRecordlevelelement(recordlevelelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldRecordlevelelementOfSpecimenListNewSpecimen != null && !oldRecordlevelelementOfSpecimenListNewSpecimen.equals(recordlevelelement)) {
                        oldRecordlevelelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldRecordlevelelementOfSpecimenListNewSpecimen = em.merge(oldRecordlevelelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = recordlevelelement.getIdrecordlevelelement();
                if (findRecordlevelelement(id) == null) {
                    throw new NonexistentEntityException("The recordlevelelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Recordlevelelement recordlevelelement;
            try {
                recordlevelelement = em.getReference(Recordlevelelement.class, id);
                recordlevelelement.getIdrecordlevelelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The recordlevelelement with id " + id + " no longer exists.", enfe);
            }
            Type type = recordlevelelement.getType();
            if (type != null) {
                type.getRecordlevelelementList().remove(recordlevelelement);
                type = em.merge(type);
            }
            Ownerinstitution ownerinstitution = recordlevelelement.getOwnerinstitution();
            if (ownerinstitution != null) {
                ownerinstitution.getRecordlevelelementList().remove(recordlevelelement);
                ownerinstitution = em.merge(ownerinstitution);
            }
            Language language = recordlevelelement.getLanguage();
            if (language != null) {
                language.getRecordlevelelementList().remove(recordlevelelement);
                language = em.merge(language);
            }
            Institutioncode institutioncode = recordlevelelement.getInstitutioncode();
            if (institutioncode != null) {
                institutioncode.getRecordlevelelementList().remove(recordlevelelement);
                institutioncode = em.merge(institutioncode);
            }
            Dataset dataset = recordlevelelement.getDataset();
            if (dataset != null) {
                dataset.getRecordlevelelementList().remove(recordlevelelement);
                dataset = em.merge(dataset);
            }
            Collectioncode collectioncode = recordlevelelement.getCollectioncode();
            if (collectioncode != null) {
                collectioncode.getRecordlevelelementList().remove(recordlevelelement);
                collectioncode = em.merge(collectioncode);
            }
            Basisofrecord basisofrecord = recordlevelelement.getBasisofrecord();
            if (basisofrecord != null) {
                basisofrecord.getRecordlevelelementList().remove(recordlevelelement);
                basisofrecord = em.merge(basisofrecord);
            }
            List<Dynamicproperty> dynamicpropertyList = recordlevelelement.getDynamicpropertyList();
            for (Dynamicproperty dynamicpropertyListDynamicproperty : dynamicpropertyList) {
                dynamicpropertyListDynamicproperty.getRecordlevelelementList().remove(recordlevelelement);
                dynamicpropertyListDynamicproperty = em.merge(dynamicpropertyListDynamicproperty);
            }
            List<Specimen> specimenList = recordlevelelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setRecordlevelelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(recordlevelelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Recordlevelelement> findRecordlevelelementEntities() {
        return findRecordlevelelementEntities(true, -1, -1);
    }

    public List<Recordlevelelement> findRecordlevelelementEntities(int maxResults, int firstResult) {
        return findRecordlevelelementEntities(false, maxResults, firstResult);
    }

    private List<Recordlevelelement> findRecordlevelelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Recordlevelelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Recordlevelelement findRecordlevelelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Recordlevelelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getRecordlevelelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Recordlevelelement> rt = cq.from(Recordlevelelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
